import { defineStore } from 'pinia'
import request from '@/utils/request'
import { ApiRes } from '@/types/data'
import { GoodsInfo, SkuType } from '@/types/goods'

export default defineStore('goods', {
  state() {
    return {
      goodsInfo: {} as GoodsInfo
    }
  },
  actions: {
    async getGoodsInfo(id: string) {
      const res = await request.get<ApiRes<GoodsInfo>>(`/goods?id=${id}`)
      this.goodsInfo = res.data.result
    },

    resetGoodsInfo() {
      this.goodsInfo = {} as GoodsInfo
    }
  },
  getters: {
    filterSkus(): SkuType[] {
      return this.goodsInfo.skus.filter((sku) => sku.inventory > 0)
    }
  }
})
